<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.HistoricActivityInstanceEntity">

  <!-- HISTORIC ACTIVITY INSTANCE INSERT -->

  <insert id="insertHistoricActivityInstanceEvent" parameterType="org.camunda.bpm.engine.impl.history.event.HistoricActivityInstanceEventEntity">
      insert into ${prefix}ACT_HI_ACTINST (
        ID_,
        PARENT_ACT_INST_ID_,
        PROC_DEF_KEY_,
        PROC_DEF_ID_,
        ROOT_PROC_INST_ID_,
        PROC_INST_ID_,
        EXECUTION_ID_,
        ACT_ID_,
        TASK_ID_,
        CALL_PROC_INST_ID_,
        CALL_CASE_INST_ID_,
        ACT_NAME_,
        ACT_TYPE_,
        ASSIGNEE_,
        START_TIME_,
        END_TIME_,
        DURATION_,
        ACT_INST_STATE_,
        SEQUENCE_COUNTER_,
        TENANT_ID_,
        REMOVAL_TIME_
      ) values (
        #{id ,jdbcType=VARCHAR},
        #{parentActivityInstanceId ,jdbcType=VARCHAR},
        #{processDefinitionKey, jdbcType=VARCHAR},
        #{processDefinitionId, jdbcType=VARCHAR},
        #{rootProcessInstanceId, jdbcType=VARCHAR},
        #{processInstanceId, jdbcType=VARCHAR},
        #{executionId, jdbcType=VARCHAR},
        #{activityId ,jdbcType=VARCHAR},
        #{taskId ,jdbcType=VARCHAR},
        #{calledProcessInstanceId ,jdbcType=VARCHAR},
        #{calledCaseInstanceId ,jdbcType=VARCHAR},
        #{activityName ,jdbcType=VARCHAR},
        #{activityType ,jdbcType=VARCHAR},
        #{taskAssignee ,jdbcType=VARCHAR},
        #{startTime, jdbcType=TIMESTAMP},
        #{endTime, jdbcType=TIMESTAMP},
        #{durationInMillis ,jdbcType=BIGINT},
        #{activityInstanceState,jdbcType=INTEGER},
        #{sequenceCounter,jdbcType=BIGINT},
        #{tenantId, jdbcType=VARCHAR},
        #{removalTime, jdbcType=TIMESTAMP}
      )
  </insert>

  <!-- HISTORIC ACTIVITY INSTANCE UPDATE -->

  <update id="updateHistoricActivityInstanceEvent" parameterType="org.camunda.bpm.engine.impl.persistence.entity.HistoricActivityInstanceEntity">
    UPDATE
        ${prefix}ACT_HI_ACTINST
    SET
      EXECUTION_ID_ = #{executionId, jdbcType=VARCHAR},
      PROC_DEF_KEY_ = #{processDefinitionKey, jdbcType=VARCHAR},
      PROC_DEF_ID_ = #{processDefinitionId, jdbcType=VARCHAR},
      ACT_ID_ = #{activityId, jdbcType=VARCHAR},
      ACT_NAME_ = #{activityName, jdbcType=VARCHAR},
      ACT_TYPE_ = #{activityType, jdbcType=VARCHAR},
      PARENT_ACT_INST_ID_ = #{parentActivityInstanceId, jdbcType=VARCHAR}
      <if test="calledProcessInstanceId != null">
        , CALL_PROC_INST_ID_ = #{calledProcessInstanceId, jdbcType=VARCHAR}
      </if>
      <if test="eventType == 'update'">
        , ASSIGNEE_ = #{taskAssignee, jdbcType=VARCHAR}
        , TASK_ID_ = #{taskId, jdbcType=VARCHAR}
      </if>
      <if test="endTime!=null">
        , END_TIME_ = #{endTime, jdbcType=TIMESTAMP}
        , DURATION_ = #{durationInMillis, jdbcType=BIGINT}
        , ACT_INST_STATE_ = #{activityInstanceState, jdbcType=INTEGER}
      </if>
    WHERE
      ID_ = #{id, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricActivityInstancesByRootProcessInstanceId"
          parameterType="java.util.Map">
    update ${prefix}ACT_HI_ACTINST set
    REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}

    where ROOT_PROC_INST_ID_ = #{rootProcessInstanceId, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricActivityInstancesByRootProcessInstanceId_mssql"
          parameterType="java.util.Map">
    update RES set
    RES.REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}
    FROM ${prefix}ACT_HI_ACTINST RES WITH (FORCESEEK)
    where ROOT_PROC_INST_ID_ = #{rootProcessInstanceId, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricActivityInstancesByProcessInstanceId"
          parameterType="java.util.Map">
    update ${prefix}ACT_HI_ACTINST
      set REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}
      where PROC_INST_ID_ = #{processInstanceId, jdbcType=VARCHAR}
  </update>

  <update id="updateHistoricActivityInstancesByProcessInstanceId_mssql"
          parameterType="java.util.Map">
    update RES set
    RES.REMOVAL_TIME_ = #{removalTime, jdbcType=TIMESTAMP}
    FROM ${prefix}ACT_HI_ACTINST RES WITH (FORCESEEK)
    where RES.PROC_INST_ID_ = #{processInstanceId, jdbcType=VARCHAR}
  </update>

  <!-- HISTORIC ACTIVITY INSTANCE SELECT -->

  <select id="selectHistoricActivityInstanceEvent" parameterType="java.lang.String" resultMap="historicActivityInstanceResultMap">
    select HAI.START_TIME_ from ${prefix}ACT_HI_ACTINST HAI
    where HAI.ID_ = #{id, jdbcType=VARCHAR}
  </select>

  <!-- HISTORIC ACTIVITY INSTANCE DELETE -->

  <delete id="deleteHistoricActivityInstancesByProcessInstanceIds">
    delete from ${prefix}ACT_HI_ACTINST
    where
      <bind name="listOfIds" value="list"/>
      <bind name="fieldName" value="'PROC_INST_ID_'"/>
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.applyInForPaginatedCollection"/>
  </delete>

  <sql id="andWhereMinuteInDateBetweenSql">
    <if test="parameter.minuteFrom != null and parameter.minuteTo != null">
      AND ${datepart1}<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.periodUnitFunction"/>${datepart2}${date}${datepart3}
      between #{parameter.minuteFrom, jdbcType=INTEGER} and #{parameter.minuteTo, jdbcType=INTEGER}
    </if>
  </sql>

  <sql id="andWhereMinuteInDateBetweenSql_oracle">
    <if test="parameter.minuteFrom != null and parameter.minuteTo != null">
      AND ${datepart1}${date}${datepart2}<include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.periodUnitFunction"/>${datepart3}
      between #{parameter.minuteFrom, jdbcType=INTEGER} and #{parameter.minuteTo, jdbcType=INTEGER}
    </if>
  </sql>

  <delete id="deleteHistoricActivityInstancesByRemovalTime"
          parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    <bind name="date" value="'REMOVAL_TIME_'"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    delete ${limitBeforeWithoutOffset} from ${prefix}ACT_HI_ACTINST
    where REMOVAL_TIME_ &lt;= #{parameter.removalTime}
    <include refid="andWhereMinuteInDateBetweenSql"/>
    ${limitAfterWithoutOffset}
  </delete>

  <delete id="deleteHistoricActivityInstancesByRemovalTime_oracle"
          parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    <bind name="date" value="'REMOVAL_TIME_'"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    delete ${limitBeforeWithoutOffset} from ${prefix}ACT_HI_ACTINST
    where REMOVAL_TIME_ &lt;= #{parameter.removalTime}
    <include refid="andWhereMinuteInDateBetweenSql_oracle"/>
    ${limitAfterWithoutOffset}
  </delete>

  <delete id="deleteHistoricActivityInstancesByRemovalTime_postgres_or_db2"
          parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject">
    <bind name="date" value="'REMOVAL_TIME_'"/>
    <bind name="reportPeriodUnitName" value="'MINUTE'"/>
    delete from ${prefix}ACT_HI_ACTINST
    where ID_ IN
      (SELECT ID_
       FROM ${prefix}ACT_HI_ACTINST
       WHERE REMOVAL_TIME_ &lt;= #{parameter.removalTime} <include refid="andWhereMinuteInDateBetweenSql"/>
       ${limitAfterWithoutOffset})
  </delete>

  <!-- HISTORIC ACTIVITY INSTANCE RESULT MAP -->

  <resultMap id="historicActivityInstanceResultMap" type="org.camunda.bpm.engine.impl.persistence.entity.HistoricActivityInstanceEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="parentActivityInstanceId" column="PARENT_ACT_INST_ID_" jdbcType="VARCHAR" />
    <result property="processDefinitionKey" column="PROC_DEF_KEY_" jdbcType="VARCHAR" />
    <result property="processDefinitionId" column="PROC_DEF_ID_" jdbcType="VARCHAR" />
    <result property="rootProcessInstanceId" column="ROOT_PROC_INST_ID_" jdbcType="VARCHAR" />
    <result property="processInstanceId" column="PROC_INST_ID_" jdbcType="VARCHAR" />
    <result property="executionId" column="EXECUTION_ID_" jdbcType="VARCHAR" />
    <result property="activityId" column="ACT_ID_" jdbcType="VARCHAR" />
    <result property="taskId" column="TASK_ID_" jdbcType="VARCHAR" />
    <result property="calledProcessInstanceId" column="CALL_PROC_INST_ID_" jdbcType="VARCHAR" />
    <result property="calledCaseInstanceId" column="CALL_CASE_INST_ID_" jdbcType="VARCHAR" />
    <result property="activityName" column="ACT_NAME_" jdbcType="VARCHAR" />
    <result property="activityType" column="ACT_TYPE_" jdbcType="VARCHAR" />
    <result property="taskAssignee" column="ASSIGNEE_" jdbcType="VARCHAR" />
    <result property="startTime" column="START_TIME_" jdbcType="TIMESTAMP" />
    <result property="endTime" column="END_TIME_" jdbcType="TIMESTAMP" />
    <result property="durationInMillis" column="DURATION_" jdbcType="BIGINT" />
    <result property="activityInstanceState" column="ACT_INST_STATE_" jdbcType="INTEGER" />
    <result property="sequenceCounter" column="SEQUENCE_COUNTER_" jdbcType="BIGINT" />
    <result property="tenantId" column="TENANT_ID_" jdbcType="VARCHAR" />
    <result property="removalTime" column="REMOVAL_TIME_" jdbcType="TIMESTAMP"/>
  </resultMap>

  <!-- HISTORIC ACTIVITY INSTANCE SELECT -->

  <select id="selectHistoricActivityInstance" resultMap="historicActivityInstanceResultMap">
    select * from ${prefix}ACT_HI_ACTINST where Id_ = #{id}
  </select>

  <select id="selectHistoricActivityInstancesByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.HistoricActivityInstanceQueryImpl" resultMap="historicActivityInstanceResultMap">
    <include refid="org.camunda.bpm.engine.impl.persistence.entity.Commons.bindOrderBy"/>
    ${limitBefore}
    select
    <if test="authCheck.shouldPerformAuthorizatioCheck">
    ${distinct}
    </if>
    RES.*
    ${limitBetween}
    <include refid="selectHistoricActivityInstancesByQueryCriteriaSql"/>
    ${orderBy}
    ${limitAfter}
  </select>

  <select id="selectHistoricActivityInstanceCountByQueryCriteria" parameterType="org.camunda.bpm.engine.impl.HistoricActivityInstanceQueryImpl" resultType="long">
    <choose>
      <when test="authCheck.shouldPerformAuthorizatioCheck">
        ${countDistinctBeforeStart} RES.ID_ ${countDistinctBeforeEnd}
      </when>
      <otherwise>
        select count(RES.ID_)
      </otherwise>
    </choose>
    <include refid="selectHistoricActivityInstancesByQueryCriteriaSql"/>
    <if test="authCheck.shouldPerformAuthorizatioCheck">
      ${countDistinctAfterEnd}
    </if>
  </select>

  <sql id="selectHistoricActivityInstancesByQueryCriteriaSql">
    FROM ${prefix}ACT_HI_ACTINST RES

    <if test="authCheck.shouldPerformAuthorizatioCheck &amp;&amp; !authCheck.revokeAuthorizationCheckEnabled &amp;&amp; authCheck.authUserId != null">
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.authCheckJoinWithoutOnClause"/>
      AUTH ON (AUTH.RESOURCE_ID_ ${authJoinStart}
        RES.PROC_DEF_KEY_ ${authJoinSeparator} '*'
        <if test="authCheck.isHistoricInstancePermissionsEnabled">
          ${authJoinSeparator} RES.PROC_INST_ID_
        </if>
        ${authJoinEnd}
      )
    </if>

    <where>
      <if test="processInstanceId != null">
        RES.PROC_INST_ID_ = #{processInstanceId}
      </if>
      <if test="activityInstanceId != null">
        and RES.ID_ = #{activityInstanceId}
      </if>
      <if test="executionId != null">
        and RES.EXECUTION_ID_ = #{executionId}
      </if>
      <if test="processDefinitionId != null">
        and RES.PROC_DEF_ID_ = #{processDefinitionId}
      </if>
      <if test="activityId != null">
        and RES.ACT_ID_ = #{activityId}
      </if>
      <if test="activityName != null">
        and RES.ACT_NAME_ = #{activityName}
      </if>
      <if test="activityNameLike != null">
        and RES.ACT_NAME_ like #{activityNameLike} ESCAPE ${escapeChar}
      </if>
      <if test="activityType != null">
        and RES.ACT_TYPE_ = #{activityType}
      </if>
      <if test="assignee != null">
        and RES.ASSIGNEE_ = #{assignee}
      </if>
      <if test="unfinished">
        and RES.END_TIME_ is null
      </if>
      <if test="finished">and RES.END_TIME_ is not null
      </if>
      <if test="startedBefore != null">
        and RES.START_TIME_ &lt;= #{startedBefore}
      </if>
      <if test="startedAfter != null">
        and RES.START_TIME_ &gt;= #{startedAfter}
      </if>
      <if test="finishedBefore != null">
        and RES.END_TIME_ &lt;= #{finishedBefore}
      </if>
      <if test="finishedAfter != null">
        and RES.END_TIME_ &gt;= #{finishedAfter}
      </if>
      <if test="activityInstanceState != null">
        and RES.ACT_INST_STATE_ = #{activityInstanceState.stateCode}
      </if>
      <if test="isTenantIdSet">
        <if test="tenantIds != null &amp;&amp; tenantIds.length > 0">
          and RES.TENANT_ID_ in
          <foreach item="tenantId" index="index" collection="tenantIds"
                   open="(" separator="," close=")">
            #{tenantId}
          </foreach>
        </if>
        <if test="tenantIds == null">
          and RES.TENANT_ID_ is null
        </if>
      </if>

      <if test="authCheck.shouldPerformAuthorizatioCheck &amp;&amp; authCheck.authUserId != null">
        <include refid="org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity.queryAuthorizationCheck"/>
      </if>
      <include refid="org.camunda.bpm.engine.impl.persistence.entity.TenantEntity.queryTenantCheck"/>

    </where>
  </sql>

  <select id="selectHistoricActivityInstanceByNativeQuery" parameterType="java.util.Map" resultMap="historicActivityInstanceResultMap">
    <if test="resultType == 'LIST_PAGE'">
      ${limitBefore}
    </if>
    ${sql}
    <if test="resultType == 'LIST_PAGE'">
      ${limitAfter}
    </if>
  </select>

  <select id="selectHistoricActivityInstanceByNativeQuery_mssql_or_db2" parameterType="java.util.Map" resultMap="historicActivityInstanceResultMap">
    <if test="resultType == 'LIST_PAGE'">
        ${limitBeforeNativeQuery}
    </if>
    ${sql}
    <if test="resultType == 'LIST_PAGE'">
      ${limitAfter}
    </if>
  </select>

  <select id="selectHistoricActivityInstanceCountByNativeQuery" parameterType="java.util.Map" resultType="long">
    ${sql}
  </select>

  <select id="selectCompletedHistoricActivityPage" resultMap="historicActivityInstanceResultMap" parameterType="java.util.Map" >
    <bind name="maxResults" value="parameter.maxResults" />
    ${optimizeLimitBeforeWithoutOffset}
    select ${limitBeforeWithoutOffset} * from ${prefix}ACT_HI_ACTINST RES

    <where>
      <if test="parameter.finishedAfter != null">
        and RES.END_TIME_ &gt; #{parameter.finishedAfter}
      </if>
      <if test="parameter.finishedAt != null">
        and RES.END_TIME_ = #{parameter.finishedAt}
      </if>
      <if test="parameter.finishedAfter == null and parameter.finishedAt == null">
        and RES.END_TIME_ is not null
      </if>
    </where>

    ORDER BY RES.END_TIME_ ASC
    ${optimizeLimitAfterWithoutOffset}
  </select>

  <select id="selectRunningHistoricActivityPage" resultMap="historicActivityInstanceResultMap"
          parameterType="java.util.Map">
    <bind name="maxResults" value="parameter.maxResults"/>
    ${optimizeLimitBeforeWithoutOffset}
    select ${limitBeforeWithoutOffset} * from ${prefix}ACT_HI_ACTINST RES

    <where>
      <if test="parameter.startedAfter != null">
        and RES.START_TIME_ &gt; #{parameter.startedAfter}
      </if>
      <if test="parameter.startedAt != null">
        and RES.START_TIME_ = #{parameter.startedAt}
      </if>
      and RES.END_TIME_ is null
    </where>

    ORDER BY RES.START_TIME_ ASC
    ${optimizeLimitAfterWithoutOffset}
  </select>

</mapper>
